Skip to content

Offer codemode for high-effort chat research turns (staging only) - #1219

Draft
NielsRogge wants to merge 8 commits into
mainfrom
claude/chat-codemode
Draft

NielsRogge wants to merge 8 commits into
mainfrom
claude/chat-codemode

Conversation

@NielsRogge

@NielsRogge NielsRogge commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds Pi's codemode to chat research turns, behind chat_codemode_enabled (default off). It applies only to the reasoning efforts in chat_codemode_reasoning_efforts (default high and max). Staging only: do not merge without explicit production authorization (chat change).

When a turn qualifies, sandbox-free MCP research turns get Pi 1.0's codemode tool. No Pi upgrade, contract change or Modal image change is needed:

  • Catalog tools inside scripts: the 21 PwC catalog tools are registered and then deactivated. The model sees only codemode and their typed declarations, and scripts call tools.pwc_*() in QuickJS and get structured results. Only the script's output reaches the model. search_web, Excalidraw and file tools stay direct calls. Poster and paper authoring turns never get codemode.
  • Prompt: <codemode_usage> treats a script as a whole research step. It includes a worked task → benchmarks → leaderboards → papers example checked against the live catalog, requires allSettled, and asks for only the fields the answer cites.
  • Budgets:
    • Each script counts as one call and one round.
    • The catalog calls a script makes spend their own per-turn budget: 24 calls and 1 MiB of results, under the MCP server's limit of 60 requests per minute per session.
    • Searches keep their ceiling.
    • A spent script budget refuses only further scripts and script catalog calls, and refused scripts still count toward the turn's call limit.
  • Events and UI: nested tool_call_start events carry parent_id, which the backend validates and persists. The chat UI groups each script's lookups under one live codemode card, with the script and its output folded away.
  • The backend sends the flag in the run frame (codemode_enabled) only for qualifying turns. With the setting off, behaviour is unchanged.

Results on staging (Luna, 24 research prompts)

Two A/B pairs at medium effort, run in the order off → on → on → off, 96 turns per arm, paired by prompt with 95% bootstrap intervals:

off on change
mean time per turn 20.6 s 23.9 s +15.7% (+1.9% … +30.8%)
first tool call 2.60 s 3.16 s +21.5%
model calls 4.39 4.52 flat
cost per turn $0.00545 $0.00442 −18.8% (−24% … −13%)
catalog calls 6.3 9.5 +51%

Luna already makes parallel direct calls, so scripts don't save model round trips. They trade latency for cost and more evidence per turn. Hence the effort gate: codemode is offered only where cost matters more than latency. The comments on this PR have the full history, including prompt v1 (no win), the shared-budget refusals, and the budget fix.

Validation

  • Worker: a new codemode.test.ts runs a real Pi session with a fake Responses provider and a model-written parallel script. Unit tests cover the run frame, prompt, script budget, retries and byte limits. The full suite passes, apart from rare timing flakes in unrelated transcript and MCP web-search tests.
  • Frontend: ChatMessage, lifecycle and session tests, tsc and eslint pass.
  • Backend: run-frame (effort gate), protocol and persistence tests pass. Remaining failures in external_pi/turn_runner also fail on main.
  • Bugbot: every finding addressed and resolved.

State

Staging runs the staging line plus this PR, with PWC_CHAT_CODEMODE_ENABLED=true (backups staging.env.bak-20261127-codemode*). Production is untouched.

🤖 Generated with Claude Code

Behind chat_codemode_enabled (off by default), sandbox-free MCP research
turns get Pi 1.0's codemode tool. The 21 PwC catalog tools are registered
but deactivated, so the model sees only their typed declarations inside
the codemode description and calls them from QuickJS scripts, which
receive structured results. Only a script's output reaches the model.

Nested calls still pass the tool policy: each counts against the call and
search budgets, while the script itself counts one round. The worker tags
their tool_call_start events with parent_id, the backend validates and
persists it, and the chat UI groups them under one codemode card that
shows each lookup live and folds the script and its output away.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

NielsRogge added a commit that referenced this pull request Nov 27, 2026
@NielsRogge

Copy link
Copy Markdown
Collaborator Author

Staging results (2026-11-27)

Staging runs 24358155 (live staging 50d59953 + this PR, including the Bugbot fix) with PWC_CHAT_CODEMODE_ENABLED=true in /etc/paperswithcode/staging.env (backup: staging.env.bak-20261127-codemode). To turn it off, remove that line and run pwc-deploy staging 243581553282530157555ec8f2a70073a04518e2 --refresh-environment. Production is untouched and this PR stays unmerged.

The same 8 reference research prompts were run twice each on Luna at medium effort (synthetic staging users, concurrency 2):

codemode off codemode on (prompt v1)
completed 16/16 16/16
total time, median / mean 18.5 s / 20.9 s 20.2 s / 20.4 s
first tool call, median 2.45 s 2.53 s
model calls per turn 4.19 4.75
uncached input tokens per turn 29.0k 25.4k
cost per turn $0.0050 $0.0046
catalog calls per turn 5.1 6.8
failed tool calls (all turns) 1 7

Codemode as wired works, but with the v1 prompt Luna doesn't use it well. 43 of 59 scripts made exactly one catalog call, and most printed whole results (text(JSON.stringify(r))) instead of filtering them. Luna already makes parallel direct tool calls, so wrapping single calls in scripts adds a model call instead of saving one. Six of the seven failures came from one script: it used Promise.all (not allSettled), one call hit the per-turn call limit, and the other four were cancelled. The seventh was a single pwc_read_paper script.

Capacity: 8 concurrent codemode turns all completed (median 20.4 s). Memory use of all Pi worker processes (production and staging) rose by about 220 MB at peak, available host memory dipped by about 600 MB to 4.6 GB, and the 1-minute load average peaked at 2.6 on 8 vCPUs. QuickJS is not a capacity concern at this scale. The binding limits stay the OpenAI and MCP circuits.

Open before this could ship:

  • A v2 prompt that makes multi-step scripts the default: a worked example of the task → benchmarks → leaderboard → paper-info chain in one script, required allSettled, and printing only cited fields. It then needs the same A/B comparison again.
  • Staging disk is at about 2.9 GB free, below the 2.6 GiB a new Pi runtime needs once the release is built, so the next staging deploy needs pwc-cleanup first.
  • Production authorization (chat change).

Staging A/B showed Luna wrapping single catalog calls in scripts, printing
whole results and using Promise.all, which added model round trips. The
guidance now names the result fields, requires allSettled, bounds fan-out
to the call budget, and includes a worked task-to-papers script checked
against the live catalog.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NielsRogge added a commit that referenced this pull request Nov 27, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

NielsRogge added a commit that referenced this pull request Nov 27, 2026
@NielsRogge

NielsRogge commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator Author

Prompt v2 results (2026-11-27)

v2 (aa4b4627) rewrites <codemode_usage>: a script is a whole research step, Promise.allSettled is required, the result fields are named, output is limited to cited fields, and fan-out stays within the call budget. It includes a worked task → benchmarks → leaderboards → papers script checked against the live catalog. 1ffef847 fixes the example's pwc_get_task argument (Bugbot). The numbers below were measured on staging b4ec6b85, with the same 8 prompts × 2, Luna, medium effort, at concurrency 2:

off on, v1 on, v2
completed 16/16 16/16 16/16
total time, median / mean 18.5 s / 20.9 s 20.2 s / 20.4 s 16.4 s / 19.8 s
first tool call, median 2.45 s 2.53 s 3.05 s
model calls per turn 4.19 4.75 4.00
input tokens per turn (uncached) 97.5k (29.0k) 101.6k (25.4k) 81.1k (22.3k)
cost per turn $0.0050 $0.0046 $0.0041
catalog calls per turn 5.1 6.8 7.9
failed tool calls (all turns) 1 7 3
  • In v2, 26 of 48 scripts made more than one catalog call (v1: 16 of 59), with up to 10 calls in one script. The first tool call comes about 0.6 s later because the model writes the script first.
  • Answers have similar length and similar numbers of paper, benchmark and link citations per prompt (a rough check, not a graded quality evaluation).
  • The three failures were single get_paper_info calls inside allSettled scripts. The other calls in those scripts kept their results.

Read: with v2, codemode is modestly better: about 11% faster at the median, about 17% fewer input tokens, about 18% cheaper, and it gathers more evidence per turn. 16 turns per arm is a small sample, so these deltas are within plausible noise. A larger run, or a few days of staging traffic, should confirm them before production.

Staging (codemode on) is being updated to 38b552f9 (live staging line + 1ffef847). The PR stays unmerged pending production authorization.

@NielsRogge

Copy link
Copy Markdown
Collaborator Author

Larger A/B (2026-11-27): 24 prompts × 2 per arm

The prompts cover leaderboards (with size and open-weight constraints), latest papers, method explanations, reading a specific paper, conferences, organizations and frameworks. Luna at medium effort, concurrency 3, prompt v2. The on-arm ran on staging 38b552f9, the off-arm on 3d3670d9 (the automatic staging sync added production de477095; it changes no chat files). The deltas below compare per-prompt means, paired by prompt, with 95% bootstrap intervals over prompts.

metric off on change 95% CI prompts where on < off
completed 48/48 48/48
total time, mean 22.1 s 22.7 s +2.4% −13.0% … +18.8% 10/24
total time, median / p90 20.5 / 30.9 s 21.9 / 35.4 s
first tool call 2.77 s 2.86 s +3.1% −19.8% … +33.5% 10/24
model calls 4.56 4.44 −2.7% −10.9% … +6.0% 10/24
input tokens 112.7k 90.9k −19.3% −28.3% … −9.4% 19/24
uncached input tokens 31.7k 23.2k −26.9% −31.3% … −21.9% 22/24
output tokens 1,499 1,647 +9.9% −1.6% … +23.3% 9/24
cost per turn $0.00553 $0.00441 −20.3% −25.7% … −14.4% 20/24
catalog calls 6.4 9.0 +39.6% +22.9% … +61.0% 2/24
failed tool calls per turn 0.29 0.58 +100% −18.5% … +850% 3/24

A rough quality check gave about the same answers: 1,281 vs 1,342 characters, 4.4 vs 4.0 distinct cited papers, 0.8 vs 0.75 benchmarks, 1.0 vs 1.25 links, and 3 vs 2 answers without citations.

Conclusions

  • No latency or round-trip win. Total time, time to first tool call and model calls are all flat, with intervals straddling zero. Luna already batches parallel direct tool calls, so codemode doesn't remove model round trips here.
  • A real cost win of about 20%. Large catalog results stay inside the script, so 19–27% fewer input tokens reach the model.
  • Much more catalog traffic: about 40% more calls per turn. That adds MCP and API load, and scripts reach the 12-call turn budget more often. 20 of the 28 on-arm failures were "Execution limit reached" refusals, mostly the last paper lookups of a fan-out. Shipping this would need the call budget rethought for scripts (for example, count catalog bytes returned rather than calls) and a check of the MCP per-client limits under scripted fan-out.

The earlier 8-prompt run's −11% median latency did not replicate. It was noise.

Staging is being switched back to codemode on, on the active release. The PR stays unmerged.

The 48-turn staging A/B showed scripts fanning out into the shared
12-call budget: 20 of 28 failures were budget refusals of the last paper
lookups. Script calls now spend a separate per-turn budget of 24 catalog
calls and 1 MiB of returned results, below the hosted MCP server's 60
requests per minute per chat session. Each codemode script still counts
as one call and round, and searches keep their own ceiling.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NielsRogge added a commit that referenced this pull request Nov 27, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Conflicts:
#	backend/chat_sandbox_worker/pi_worker/src/policy.ts

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

A spent script budget now refuses further script catalog calls and new
codemode scripts with its own message, instead of closing tooling for the
whole turn, so web search and diagrams keep their own budgets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NielsRogge added a commit that referenced this pull request Nov 27, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Conflicts:
#	backend/chat_sandbox_worker/pi_worker/src/policy.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1f49f5c. Configure here.

NielsRogge added a commit that referenced this pull request Nov 27, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>

# Conflicts:
#	backend/chat_sandbox_worker/pi_worker/src/policy.ts
@NielsRogge

Copy link
Copy Markdown
Collaborator Author

Script budget A/B (2026-11-27)

bc3e7e36 + 1f49f5c7 + f7cf99e2 give script catalog calls their own per-turn budget: 24 calls and 1 MiB of results, under the MCP server's limit of 60 requests per minute per chat session. Each script still counts as one model call and one round. A spent script budget refuses only script calls and new scripts; other tools keep their own budgets, and refused retries still reach the turn's call limit. Bugbot is clean on f7cf99e2.

Same 24 prompts × 2, Luna at medium effort, concurrency 3, on staging ab25b572. Deltas are paired by prompt, with 95% bootstrap intervals.

metric off on (new budget) change vs off 95% CI
completed 48/48 48/48
total time, mean 22.1 s 25.4 s +14.8% −5.5% … +37.3%
total time, median / p90 20.5 / 30.9 s 22.6 / 38.0 s
model calls 4.56 4.54 −0.5% −11.1% … +11.9%
uncached input tokens 31.7k 23.6k −25.7% −30.8% … −20.1%
cost per turn $0.00553 $0.00447 −19.2% −25.9% … −12.4%
catalog calls 6.4 9.5 +47.4% +27.7% … +71.8%
failed tool calls per turn 0.29 0.21 −28.6% −92.9% … +150%

Against the previous on-arm, with the shared 12-call budget: failed tool calls fell 64% (−96% … −20%), and there were no script-budget refusals at all (20 before). Catalog calls changed by +5.6% (−6% … +19%). Ten turns used more than 12 catalog calls, peaking at 20. The remaining failures are real: catalog misses (for example "Google DeepMind" as an organization name), one MCP 502, three script errors, and two turn-limit refusals of a further codemode call.

Read: the budget fixed the refusals, and codemode now fails less often than direct calls while staying about 19% cheaper. Its catalog traffic is about 47% higher, and latency is not better: the mean is up 15%, but the CI still includes no change, so this run cannot say whether codemode is slower. Before production, it would be worth running one more pair of arms to settle latency, or capping script fan-out lower if latency matters more than evidence gathered.

Staging keeps codemode on (ab25b572). The PR stays unmerged pending production authorization.

@NielsRogge

Copy link
Copy Markdown
Collaborator Author

Latency settled: second A/B pair (2026-11-27)

I ran a second pair with the script budget on staging ab25b572. Across both pairs the order was off → on → on → off, so drift during the day affects both arms. Results are pooled at 96 turns per arm (24 prompts × 4), paired by prompt, with 95% bootstrap intervals:

metric off on change 95% CI
completed 96/96 96/96
total time, mean 20.6 s 23.9 s +15.7% +1.9% … +30.8%
total time, median / p90 20.2 / 28.4 s 22.4 / 35.0 s
first tool call 2.60 s 3.16 s +21.5% +0.9% … +45.3%
model calls 4.39 4.52 +3.1% −5.3% … +12.7%
uncached input tokens 31.4k 23.3k −25.6% −30.3% … −20.4%
cost per turn $0.00545 $0.00442 −18.8% −24.4% … −12.9%
catalog calls 6.3 9.5 +50.8% +38.6% … +66.3%
failed tool calls per turn 0.32 0.19 −41.9% −96.4% … +38.1%

The second pair alone gives +16.7% (+7.4% … +27.1%) for total time, consistent with the first (+14.8%). The two off-arms differ from each other by 13%, so run-to-run drift is real, and the alternating order is what makes the pooled result trustworthy.

Where the time goes: of the 3.2 s added per turn, about 1.4 s is extra model time (writing the script), and about 1.8 s is spent outside the model running about 50% more catalog calls, often as sequential chains through the per-turn concurrency gate of 3.

Conclusion: for this chat with Luna, codemode trades about 16% more latency, and a slower first tool call, for about 19% lower cost and fewer failed calls. It does not reduce model round trips, because Luna already makes parallel direct calls. I would not ship it as the default for latency-sensitive chat. Options if the cost saving matters:

  1. Cap script fan-out (for example 12 catalog calls per turn) to trade some evidence for latency.
  2. Raise the per-turn MCP gate from 3 toward the server's 4 per client.
  3. Keep codemode for high-effort turns only, where cost matters more than latency.

Staging is being switched back to codemode on (ab25b572). The PR stays unmerged.

Two paired staging A/B runs (96 turns per arm at medium effort) showed
codemode adds about 16% latency for about 19% lower cost. Keep it for the
efforts where cost matters more than latency, configurable through
chat_codemode_reasoning_efforts (default high and max).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NielsRogge added a commit that referenced this pull request Nov 27, 2026
@NielsRogge NielsRogge changed the title Let chat research turns batch catalog calls in codemode scripts (staging only) Offer codemode for high-effort chat research turns (staging only) Nov 27, 2026
@NielsRogge

Copy link
Copy Markdown
Collaborator Author

High-effort A/B (2026-11-27)

Same 24 prompts at high effort, on staging e3a1886e with the effort gate. Four runs of 48 turns in the order on → off → off → on, with each flag switch checked by a high-effort test turn. Deltas are paired by prompt, with 95% bootstrap intervals.

Incomplete run: in the second off run, 31 of 48 turns were refused at admission by staging's own OpenAI rate-limit circuit (openai_global_rate_limited, the per-10-minute call window). This is the PwC circuit, not OpenAI. Those turns never ran and are excluded. The first pair is complete and leads to the same conclusion.

metric off on change 95% CI
first pair (48 vs 48), total time 26.9 s 29.7 s +10.4% +2.2% … +18.8%
first pair, cost per turn $0.00620 $0.00502 −19.1% −24.3% … −13.2%
pooled (65 vs 96), total time 27.0 s 30.9 s +14.6% +5.5% … +24.4%
pooled, median / p90 26.8 / 40.1 s 29.8 / 46.0 s
pooled, first tool call 3.15 s 3.65 s +16.0% +3.4% … +30.2%
pooled, model calls 4.63 4.71 +1.6% −6.8% … +11.1%
pooled, output tokens 2,287 2,753 +20.4% +8.9% … +32.6%
pooled, uncached input tokens 33.5k 25.0k −25.5% −30.7% … −19.2%
pooled, cost per turn $0.00618 $0.00526 −14.8% −20.6% … −8.2%
pooled, catalog calls 6.7 10.3 +54.2% +38.1% … +75.0%
pooled, failed tool calls per turn 0.33 0.23 −29.8% −80.4% … +50.0%

Read: high effort behaves like medium effort. Codemode costs about 10–15% more latency, saves about 15–19%, and makes about 54% more catalog calls. Model round trips are flat again. High effort writes about 20% more output tokens with codemode (the scripts), so the relative cost saving is a little smaller than at medium. In absolute terms, codemode adds about 3–4 s to a 27 s turn at high effort, against about 3 s on a 21 s turn at medium.

The effort gate therefore doesn't change the trade-off. It only limits it to turns where users already accept longer waits. Whether about 15% cheaper is worth about 12% slower on those turns is a product call. Neither effort level shows a latency win.

Staging is back on codemode (verified). The PR stays unmerged.

@NielsRogge
NielsRogge marked this pull request as draft November 27, 2026 14:36
@NielsRogge

Copy link
Copy Markdown
Collaborator Author

Staging keeps codemode on by the user's choice (2026-11-27): PWC_CHAT_CODEMODE_ENABLED=true in /etc/paperswithcode/staging.env, applied to high and max effort turns only. The code lives on the staging line (claude/staging-codemode-50d59953, last deployed ab25b572 then synced forward). The automatic staging sync carries it through future production deploys. Production is unaffected and this PR stays a draft. To turn it off, set the flag to false and run pwc-deploy staging <active staging sha> --refresh-environment.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant